# -*- coding:utf-8 -*-

# @Time    : 2018/9/27 2:26 PM

# @Author  : Swing


import pandas as pd
import numpy as np

import seaborn as sn
import matplotlib.pyplot as plt


def analysis(train):
    # 设置参数
    params = {'legend.fontsize': 'x-large',
              'figure.figsize': (30, 10),
              'axes.labelsize': 'x-large',
              'axes.titlesize': 'x-large',
              'xtick.labelsize': 'x-large',
              'ytick.labelsize': 'x-large'
              }

    sn.set_style('whitegrid')
    sn.set_context('talk')

    plt.rcParams.update(params)
    pd.options.display.max_colwidth = 600

    # 对类别型特征，观察其取值范围及直方图
    categorical_features = ['season', 'mnth', 'weathersit', 'weekday']
    for col in categorical_features:
        print(col, '属性的不同取值和出现的次数')
        print(train[col].value_counts())
        train[col] = train[col].astype('object')

    sn.violinplot(data=train[['holiday', 'cnt']], x="holiday", y="cnt")

    # 一年每天的骑车量
    train['date'] = pd.to_datetime(train['dteday'])
    train['dayofyear'] = train['date'].dt.dayofyear

    fig, ax = plt.subplots()
    sn.pointplot(data=train[['dayofyear', 'cnt', 'yr']], x='dayofyear', y='cnt', hue='yr', ax=ax)
    ax.set(title='daily distribution of counts')

    # 季节与骑车数量的关系
    fig, ax = plt.subplots()
    sn.barplot(data=train[['season', 'cnt']], x="season", y="cnt")
    ax.set(title="Seasonly distribution of counts")

    # 月份与骑车数量的关系
    fig, ax = plt.subplots()
    sn.barplot(data=train[['mnth', 'cnt']], x='mnth', y='cnt')
    ax.set(title='Monthly distribution of counts')

    # 天气与骑车数目的关系
    fig, ax = plt.subplots()
    sn.barplot(data=train[['weathersit', 'cnt']], x='weathersit', y='cnt')
    ax.set(title='weathersit distribution of counts')

    # 工作日和节假日的分布
    fig, (ax1, ax2) = plt.subplots(ncols=2)
    sn.barplot(data=train, x='holiday', y='cnt', hue='season', ax=ax1)
    sn.barplot(data=train, x='workingday', y='cnt', hue='season', ax=ax2)

    # 相关性
    corrmatt = train[['temp', 'atemp', 'hum', 'windspeed', 'casual', 'registered', 'cnt']].corr()
    mask = np.array(corrmatt)
    mask[np.tril_indices_from(mask)] = False
    sn.heatmap(corrmatt, mask=mask, vmax=8, square=True, annot=True)

    plt.show()
